package hashmap_test;

import java.util.HashMap;
import java.util.Map;

public class test {
    public static void main(String[] args) {
        String s = "1234";
        System.out.println(s.substring(0, 2));
    }
}

class Solution {
    public String minWindow(String s, String t) {
        //定义两个哈希表存储有效数值
        Map<Character, Integer> hash1 = new HashMap<>();
        Map<Character, Integer> hash2 = new HashMap<>();
        int len = t.length();
        int kinds = 0;
        int len2 = Integer.MAX_VALUE;
        int begin = -1;

        for (int i = 0; i < len; i++) {
            //记录种类
            if (hash1.get(t.charAt(i)) == 0) {
                kinds++;
            }
            hash1.put(t.charAt(i), hash1.getOrDefault(t.charAt(i), 0) + 1);
        }

        //开始循环遍历
        for (int left = 0, right = 0, count = 0; right < s.length(); right++) {
            char in = s.charAt(right);
            //维护有效字母
            if (hash2.get(in) == hash1.get(in)) {
                count++;
            }
            hash2.put(in, hash2.getOrDefault(in, 0) + 1);
            //有效数字等于了len，就没有必要right++了
            while (count == kinds) {
                if (right - left + 1 < len2) {
                    begin = left;
                    len2 = right - left +1;
                }

                char out = s.charAt(left);
                //维护count
                if (hash2.get(out) == hash1.get(out)) {
                    count--;
                }
                //出窗口
                hash2.put(out, hash2.getOrDefault(out,0) - 1);
                left++;
            }
        }
        if(begin == -1){
            return new String();
        }else {
            return s.substring(begin,len2+begin);
        }
    }
}
